package iqq.mk.action;

import iqq.im.QQActionListener;
import iqq.im.QQException;
import iqq.im.action.AbstractHttpAction;
import iqq.im.core.QQConstants;
import iqq.im.core.QQContext;
import iqq.im.core.QQService;
import iqq.im.core.QQSession;
import iqq.im.event.QQActionEvent;
import iqq.im.http.QQHttpRequest;
import iqq.im.http.QQHttpResponse;
import iqq.im.service.HttpService;

import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Tony on 10/6/15.
 */
public class CheckMKRCodeAction extends AbstractHttpAction {
    private static final Logger LOG = LoggerFactory.getLogger(CheckMKRCodeAction.class);

    /**
     * <p>Constructor for AbstractHttpAction.</p>
     *
     * @param context  a {@link QQContext} object.
     * @param listener a {@link QQActionListener} object.
     */
    public CheckMKRCodeAction(QQContext context, QQActionListener listener) {
        super(context, listener);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected QQHttpRequest onBuildRequest() throws QQException, JSONException {
        QQHttpRequest req = createHttpRequest("GET", "http://ptlogin2.qq.com/ptqrlogin");
        //&pt_uistyle=20&aid=1600000084&
        req.addGetValue("ptredirect", "1");
        req.addGetValue("u1", "http://appimg2.qq.com/card/index_v3.html");
        req.addGetValue("h", "1");
        req.addGetValue("t", "1");
        req.addGetValue("g", "1");
        req.addGetValue("from_ui", "1");
        req.addGetValue("ptlang", "2052");
        req.addGetValue("action", "2-0-1460790924574");
        req.addGetValue("js_type", "1");
        req.addGetValue("js_ver", "10153");        
        req.addGetValue("pt_uistyle", "20");
        req.addGetValue("aid", "1600000084");
        req.addHeader("Referer", "http://xui.ptlogin2.qq.com/cgi-bin/xlogin?appid=1600000084&s_url=http%3A%2F%2Fappimg2.qq.com%2Fcard%2Findex_v3.html");
        return req;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onHttpStatusOK(QQHttpResponse response) throws QQException {
        Pattern pt = Pattern.compile(QQConstants.REGXP_LOGIN);
        Matcher mc = pt.matcher(response.getResponseString());
        LOG.info("WebLogin: " + response.getResponseString());
        if (mc.find()) {
            int ret = Integer.parseInt(mc.group(1));
            switch(ret){
                case 0: //set account qq
	            		HttpService httpService = (HttpService) getContext().getSerivce(QQService.Type.HTTP);        		
		                String uin = httpService.getCookie("uin",null).getValue().substring(1);
		                getContext().getAccount().setUin(Long.valueOf(uin));
		                getContext().getAccount().setNickname(mc.group(6));	        		
		                getContext().getSession().setState(QQSession.State.ONLINE);
	                	notifyActionEvent(QQActionEvent.Type.EVT_OK, mc.group(5));
                		break;
                case 65: throw new QQException(QQException.QQErrorCode.QRCODE_INVALID, mc.group(5));
                case 66: throw new QQException(QQException.QQErrorCode.QRCODE_OK, mc.group(5));
                case 67: throw new QQException(QQException.QQErrorCode.QRCODE_AUTH, mc.group(5));
                default: throw new QQException(QQException.QQErrorCode.INVALID_USER, mc.group(5));
            }
        } else {
            throw new QQException(QQException.QQErrorCode.UNEXPECTED_RESPONSE);
        }
    }

}
